1 00:00:00,450 --> 00:00:06,210 In this lecture, we're going to be scripting the first trap of our RB, which are wooden beams that 2 00:00:06,210 --> 00:00:08,670 break shortly after standing on them. 3 00:00:09,060 --> 00:00:11,130 So let's go ahead and get started. 4 00:00:11,460 --> 00:00:16,980 So we're going to create a new module script inside of the handler section for our starter player script. 5 00:00:16,980 --> 00:00:20,820 And I'm going to name this our would break handler. 6 00:00:20,820 --> 00:00:27,840 And this script is going to go through and grab all of the different modules in our game that are tagged 7 00:00:27,840 --> 00:00:29,850 with the wood break tag. 8 00:00:30,150 --> 00:00:35,400 And then we're going to use a class to create objects for those different models. 9 00:00:35,730 --> 00:00:38,880 So I'm going to use the same template that we've been using before. 10 00:00:38,880 --> 00:00:43,230 And I'm going to rename it to Wood Break Handler. 11 00:00:44,320 --> 00:00:49,660 And what we're going to need is an initialize function, because we're going to be listening to the 12 00:00:49,660 --> 00:00:50,830 collection service. 13 00:00:50,830 --> 00:00:55,360 So we'll get the collection service game, get service collection service. 14 00:00:55,360 --> 00:01:02,740 And we'll create a constant to store the tag that we're going to be tracking, which is the tag of would 15 00:01:02,770 --> 00:01:03,610 break. 16 00:01:04,510 --> 00:01:09,730 And inside of our initialize function, we're going to refer to the collection service and get instance 17 00:01:09,730 --> 00:01:11,860 added signal for this tag. 18 00:01:12,010 --> 00:01:15,940 And we're going to connect a function to this get that instance. 19 00:01:15,940 --> 00:01:20,770 And then we're going to use a class to create an object for this model. 20 00:01:20,770 --> 00:01:25,660 So we'll create another module script as a child of our handler script. 21 00:01:25,660 --> 00:01:31,330 And we're going to rename this one to be our breakable wood class. 22 00:01:32,500 --> 00:01:36,730 And then I'm going to be using another template which is going to be attached to this lecture. 23 00:01:36,730 --> 00:01:39,130 And this is going to be my class template. 24 00:01:39,130 --> 00:01:44,740 So it's a very simple template that basically just sets up our constructor for us as well as our destroy 25 00:01:44,740 --> 00:01:45,460 function. 26 00:01:45,460 --> 00:01:51,790 And then if we attempt to create an object again on the exact same model, we'll just return that object 27 00:01:51,790 --> 00:01:55,660 that already exists in case we find it within our cache up here. 28 00:01:55,660 --> 00:02:00,310 So we're going to rename this to our breakable wood class. 29 00:02:00,310 --> 00:02:04,930 And then we can go ahead and rename this to Breakable Wood Objects. 30 00:02:06,130 --> 00:02:09,730 And then we can go ahead and require this class from our handler. 31 00:02:09,730 --> 00:02:12,190 So we'll call it breakable wood class. 32 00:02:12,190 --> 00:02:16,900 And that's going to be equal to script dot breakable wood class. 33 00:02:16,900 --> 00:02:21,910 And then every single time an instance is added in our game with this tag, we can go ahead and refer 34 00:02:21,910 --> 00:02:27,100 to our breakable wood class and use the new constructor and pass our instance. 35 00:02:27,980 --> 00:02:32,120 So inside of this class, what we're going to need are several different services. 36 00:02:32,120 --> 00:02:37,010 We're going to need the player service to listen for when the player touches one of the parts. 37 00:02:37,010 --> 00:02:41,780 We're going to need the sound service to play the wood breaking sound. 38 00:02:41,780 --> 00:02:47,330 We're going to need the tween service to fade out or fade in our wooden beams. 39 00:02:47,330 --> 00:02:53,180 And then we're also going to need a replicated storage to be able to grab the particle emitter that's 40 00:02:53,180 --> 00:02:55,520 going to be emitting those wood particles. 41 00:02:55,520 --> 00:02:58,040 So we'll get the player service. 42 00:03:01,180 --> 00:03:04,000 We'll go ahead and get the sound service. 43 00:03:06,080 --> 00:03:08,900 We'll also get replicated storage. 44 00:03:10,750 --> 00:03:13,600 And then we're also going to get the tween service. 45 00:03:17,370 --> 00:03:21,510 Now, when the start new constructor is going to be called, we're going to get passed a model and we'll 46 00:03:21,510 --> 00:03:24,660 store a reference of that model inside of our object. 47 00:03:25,320 --> 00:03:31,410 Next, we can go ahead and store a value that's going to represent the delay before our part breaks. 48 00:03:31,410 --> 00:03:35,010 So when the player touches it we're going to wait for this delay period. 49 00:03:35,010 --> 00:03:38,250 And then we're going to destroy the wooden beam. 50 00:03:38,250 --> 00:03:40,050 So we'll call this delay. 51 00:03:40,050 --> 00:03:42,330 And it's going to be equal to the model. 52 00:03:42,330 --> 00:03:45,510 And I have an attribute on the model called delay. 53 00:03:46,470 --> 00:03:51,360 Just in case our model does not have this attribute on it, then we can go ahead and set a default delay, 54 00:03:51,360 --> 00:03:56,400 and we can go ahead and define that as a constant inside of our variable section. 55 00:03:56,580 --> 00:03:59,280 And we can go ahead and call it default delay. 56 00:03:59,280 --> 00:04:02,310 And the default delay can be like 0.5 seconds. 57 00:04:02,310 --> 00:04:04,320 And then we'll pass that here. 58 00:04:04,470 --> 00:04:10,530 So now either the delay is going to be defined on the model or we'll have a default delay. 59 00:04:11,280 --> 00:04:16,650 And then we can also go ahead and have a boolean to represent a dip out. 60 00:04:16,650 --> 00:04:18,450 So we'll call it on cooldown. 61 00:04:18,450 --> 00:04:20,820 And it's going to be false by default. 62 00:04:21,450 --> 00:04:26,490 So if we actually go ahead and take a look at all of our RB models, let me go ahead and parent these 63 00:04:26,490 --> 00:04:28,440 to our workspace. 64 00:04:28,440 --> 00:04:36,270 Here are the different wooden beams or these different brake parts that are going to be, um, destroying 65 00:04:36,270 --> 00:04:38,580 themselves when a player touches them. 66 00:04:38,580 --> 00:04:42,990 So whenever a player touches this invisible detect part, then we're going to wait for that delay. 67 00:04:42,990 --> 00:04:49,770 And then we're going to make this part, uh, non collidable and spit out those particles just to see 68 00:04:49,770 --> 00:04:53,370 if we can try and kill the player by having them falling through the map. 69 00:04:53,790 --> 00:04:59,340 But each of these brake parts have an attribute set for the delay. 70 00:04:59,340 --> 00:05:05,160 I've set 0.3 seconds for these brake parts, so every single one of these that get generated on the 71 00:05:05,160 --> 00:05:07,320 map are going to have a delay of 0.3 seconds. 72 00:05:07,320 --> 00:05:12,930 But just in case we accidentally forget to set a delay, then the default is going to be 0.5 seconds. 73 00:05:12,930 --> 00:05:18,030 And you can also see that each one of these models has that tag of wood brake. 74 00:05:18,950 --> 00:05:24,770 So what we want to do is we want to get our model and get that detect zone part and listen for when 75 00:05:24,770 --> 00:05:25,850 it is touched. 76 00:05:25,850 --> 00:05:30,920 And we can go ahead and connect a lambda function to this and get that part that has touched our detect 77 00:05:30,920 --> 00:05:31,490 zone. 78 00:05:31,490 --> 00:05:35,060 And we want to check if a player has touched this detect zone. 79 00:05:35,060 --> 00:05:40,430 So we're going to use the player service and the function get player from character and pass the parent 80 00:05:40,430 --> 00:05:41,330 of other part. 81 00:05:41,750 --> 00:05:45,020 If there is no player, then we're just going to return. 82 00:05:45,020 --> 00:05:51,890 Otherwise we can go ahead and break and we can go ahead and define two functions directly on our object. 83 00:05:51,890 --> 00:05:56,390 One can be break to break the beam, and then we can have another function. 84 00:05:56,390 --> 00:06:02,180 We could call it uh restore to restore the beam back to its original state. 85 00:06:02,180 --> 00:06:06,860 So when we touch this part then we can go ahead and call our self. 86 00:06:08,070 --> 00:06:10,170 Uh, brake function. 87 00:06:11,660 --> 00:06:16,310 And then we also want to go ahead and listen for when our model is being destroyed. 88 00:06:16,310 --> 00:06:21,320 And when that happens, then we can go ahead and call the destroy function on our object. 89 00:06:21,320 --> 00:06:26,660 That way we can go ahead and clean everything up and not leave anything sitting around in memory. 90 00:06:27,970 --> 00:06:32,830 Okay, so inside of the break function, what we want to do is we want to break the part. 91 00:06:32,830 --> 00:06:34,090 We want to emit particles. 92 00:06:34,090 --> 00:06:38,590 We want to set the can collide property to false and then make the part invisible. 93 00:06:38,590 --> 00:06:41,680 However, we don't want to break the part multiple times. 94 00:06:41,680 --> 00:06:45,310 So that's what that on cooldown debounce is going to be for. 95 00:06:45,310 --> 00:06:49,420 Since when the part is going to be touched, that touched event is going to be fired multiple times. 96 00:06:49,420 --> 00:06:50,830 We need to have a debounce. 97 00:06:50,830 --> 00:06:56,470 So if we are already broken or we're on a cooldown now, we're just going to return. 98 00:06:56,470 --> 00:07:00,580 Otherwise we're going to set on cooldown equal to true. 99 00:07:01,450 --> 00:07:04,900 And then what we need to do is we need to wait for that delay. 100 00:07:04,900 --> 00:07:08,470 So we'll wait for self dot delay before we break the part. 101 00:07:08,470 --> 00:07:13,390 And then once we do that, we're going to go ahead and play the breaking sound. 102 00:07:13,390 --> 00:07:15,940 And we can grab that from the sound service. 103 00:07:16,360 --> 00:07:20,500 So up here we can make some references to the break sound. 104 00:07:20,500 --> 00:07:25,330 So break sound is going to be equal to sound service dot SFX. 105 00:07:25,330 --> 00:07:28,330 And inside of there there's a sound called Wood Break. 106 00:07:28,330 --> 00:07:31,120 Actually let me reposition it up here. 107 00:07:31,270 --> 00:07:34,990 And then we can also go ahead and get the particle emitter from Replicated storage. 108 00:07:34,990 --> 00:07:39,490 So replicated storage dot assets dot wood break particle emitter. 109 00:07:40,510 --> 00:07:42,640 And then we can define a couple more constants. 110 00:07:42,640 --> 00:07:47,110 For example how many particles we would like to emit from the particle emitter. 111 00:07:47,110 --> 00:07:49,300 We could call this particle emission. 112 00:07:49,300 --> 00:07:51,430 And we could set it to a value like 50. 113 00:07:51,430 --> 00:07:56,410 And then we can also define a constant for how long it will take for the plank to respawn. 114 00:07:56,410 --> 00:08:01,480 So we can call this a respawn duration and set it to something like eight seconds. 115 00:08:02,630 --> 00:08:04,970 So I'm back inside of our break function. 116 00:08:04,970 --> 00:08:08,990 What we could do is we can create a clone of that break sound. 117 00:08:10,380 --> 00:08:16,470 We'll set the player on, remove property equal to true, and then set the parent of the clone equal 118 00:08:16,470 --> 00:08:17,760 to the self. 119 00:08:17,760 --> 00:08:19,860 Dot model dot break part. 120 00:08:20,630 --> 00:08:24,260 And then we can just destroy the clone to play the sound. 121 00:08:24,260 --> 00:08:29,480 And then afterwards we'll get a clone of the emitter, so we'll call it emitter clone. 122 00:08:29,810 --> 00:08:31,490 Emitter clone. 123 00:08:31,700 --> 00:08:39,170 We'll set the parent equal to self dot model dot break part. 124 00:08:39,860 --> 00:08:42,770 And then we can go ahead and emit. 125 00:08:43,330 --> 00:08:46,360 The particle emission amount out of our particle emitter. 126 00:08:46,880 --> 00:08:53,930 Then afterwards we're going to set the break part transparency equal to one. 127 00:08:53,930 --> 00:08:55,010 So it's invisible. 128 00:08:55,250 --> 00:08:57,230 And then we'll just copy this. 129 00:08:57,230 --> 00:09:00,890 And this time we're going to update the Kankali property to be false. 130 00:09:01,810 --> 00:09:07,090 And then afterwards, what we could do is we could have a wait statement to wait for the emitter clone's 131 00:09:07,090 --> 00:09:08,560 particles to all disappear. 132 00:09:08,560 --> 00:09:14,410 So we refer to emitter clone, we get the lifetime property and then get the max number in that range. 133 00:09:14,410 --> 00:09:19,180 Then we can go ahead and destroy that emitter clone because we don't need it anymore. 134 00:09:20,100 --> 00:09:25,830 And then after we have broken the part, then we want to go ahead and wait for that delay and then restore 135 00:09:25,830 --> 00:09:27,690 the part back to its original state. 136 00:09:27,690 --> 00:09:35,280 So before we wait down here for the particle emitter, we can use task dot delay to delay a function 137 00:09:35,280 --> 00:09:36,270 from occurring. 138 00:09:36,270 --> 00:09:39,240 And we're going to wait for that respawn duration. 139 00:09:39,240 --> 00:09:43,320 And the function we're going to be calling is the self restore function. 140 00:09:43,320 --> 00:09:49,710 Now since we're using a colon here, but we want to pass the function to our task dot delay function. 141 00:09:49,710 --> 00:09:52,710 We're going to index with restore. 142 00:09:53,280 --> 00:09:59,370 And then we need to pass self to the restore function as you know the self parameter. 143 00:10:00,260 --> 00:10:06,890 And then inside of our restore function, what we can go ahead and do is we first need to check whether 144 00:10:06,890 --> 00:10:08,960 or not this model has been destroyed. 145 00:10:08,960 --> 00:10:13,580 So if we don't have a reference to the model. 146 00:10:14,870 --> 00:10:21,770 Or if we do have a reference to the model, but the parent is equal to nil. 147 00:10:22,850 --> 00:10:30,440 Or let's say we cannot find inside of our model the break part for some reason. 148 00:10:30,440 --> 00:10:35,390 Then we're just going to return because we're yielding for those eight seconds. 149 00:10:35,390 --> 00:10:39,800 The player could have walked far enough away where the entire model gets deleted. 150 00:10:39,800 --> 00:10:45,500 So we want to make sure that we don't try or attempt to restore this object that's already been deleted. 151 00:10:45,500 --> 00:10:46,910 We're just going to return. 152 00:10:47,810 --> 00:10:54,620 Otherwise, what we could go ahead and do is we could use the tween service to fade that beam back to 153 00:10:54,620 --> 00:10:55,520 its original state. 154 00:10:55,520 --> 00:10:58,160 So we'll get the break part. 155 00:10:58,940 --> 00:11:06,620 We'll do a tween info of, for example, one second and we want to update the transparency equal to 156 00:11:06,620 --> 00:11:08,180 zero to be fully opaque. 157 00:11:08,180 --> 00:11:09,710 We'll play that tween. 158 00:11:09,980 --> 00:11:12,290 We'll set self dot model dot break. 159 00:11:12,290 --> 00:11:16,040 Part dot can collide back to true. 160 00:11:16,040 --> 00:11:21,470 And then we can go ahead and set self dot on cooldown equal to false. 161 00:11:21,470 --> 00:11:23,690 Once this tween finishes playing. 162 00:11:23,690 --> 00:11:26,450 So actually we'll get the tween. 163 00:11:27,550 --> 00:11:29,380 From the Tween Service. 164 00:11:30,690 --> 00:11:34,230 And then we can go ahead and play the tween. 165 00:11:35,170 --> 00:11:37,570 And then when that twin completes. 166 00:11:39,260 --> 00:11:43,820 Then we can go ahead and set self on cooldown equal to false. 167 00:11:44,720 --> 00:11:52,820 Okay, that was a lot of code to write, so we should have everything set up we need for our, uh, 168 00:11:52,820 --> 00:11:54,440 breakable wood trap. 169 00:11:55,010 --> 00:11:57,590 So let's go ahead and test it out. 170 00:11:57,590 --> 00:12:02,780 And before we do that, we need to move all of these Obby models back into replicated storage. 171 00:12:02,780 --> 00:12:08,210 So we'll get all those children, and then we'll go ahead and set the parent back to the Obby models 172 00:12:08,210 --> 00:12:08,900 folder. 173 00:12:09,230 --> 00:12:10,970 And now we can go ahead and play test. 174 00:12:13,320 --> 00:12:14,370 And perfect. 175 00:12:14,370 --> 00:12:16,950 The first ones we got here are the. 176 00:12:17,820 --> 00:12:18,780 Uh, bridges. 177 00:12:18,780 --> 00:12:22,500 So if we go and step on this, there we go. 178 00:12:22,500 --> 00:12:23,220 It breaks. 179 00:12:23,220 --> 00:12:24,480 It has disappeared. 180 00:12:24,480 --> 00:12:29,370 And then if we wait eight seconds, it should fade back and come to its original state. 181 00:12:29,370 --> 00:12:31,080 So we're just going to sit here and wait. 182 00:12:33,130 --> 00:12:34,570 Hopefully it'll fade in. 183 00:12:35,950 --> 00:12:38,500 And unfortunately it does not look like it's restoring itself. 184 00:12:38,500 --> 00:12:40,360 So let's go ahead and see what we did wrong. 185 00:12:40,360 --> 00:12:41,500 Exactly. 186 00:12:43,220 --> 00:12:47,480 So perhaps maybe it doesn't like us doing this. 187 00:12:47,480 --> 00:12:49,070 So maybe an alternative. 188 00:12:49,070 --> 00:12:55,250 What we could do is we could just pass our function here and then just call self Restore instead. 189 00:12:55,370 --> 00:12:56,960 Maybe that'll work. 190 00:12:58,600 --> 00:13:01,060 Um, yeah, I'll just try it out. 191 00:13:03,890 --> 00:13:05,030 So there we go. 192 00:13:05,030 --> 00:13:05,660 We get our bridge. 193 00:13:05,660 --> 00:13:07,460 If we touch it, it breaks. 194 00:13:07,460 --> 00:13:09,830 And then let's go ahead and see if it restores itself. 195 00:13:12,030 --> 00:13:16,920 And unfortunately still not restoring itself so there could be an issue. 196 00:13:16,950 --> 00:13:22,740 A respawn duration is set to eight seconds, so it shouldn't be taking that long, but it appears that 197 00:13:22,740 --> 00:13:29,700 our restore function is not actually executing, and it appears that there could be an issue with this 198 00:13:29,700 --> 00:13:31,110 return statement right here. 199 00:13:31,170 --> 00:13:34,110 It could be returning prematurely for some reason. 200 00:13:34,110 --> 00:13:39,180 So if there isn't a model or the parent is equal to nil. 201 00:13:39,900 --> 00:13:40,440 Or. 202 00:13:40,440 --> 00:13:44,760 Oh, that's why we forgot to include a Not statement here. 203 00:13:45,840 --> 00:13:53,370 If there isn't a break part because we had the not not there, it found the break part and it was returning. 204 00:13:53,370 --> 00:13:55,860 So we got to put that not statement there. 205 00:13:57,020 --> 00:14:01,460 That means we should be able to restore this back to self restore and then pass self. 206 00:14:01,460 --> 00:14:03,920 So let's go ahead and try that one more time. 207 00:14:05,840 --> 00:14:08,450 Forget our wood bridge over there. 208 00:14:08,450 --> 00:14:09,950 We'll go walk over to it. 209 00:14:12,920 --> 00:14:17,870 And then we go and step on it, breaks it, and then it should restore itself. 210 00:14:20,310 --> 00:14:22,080 Any second now. 211 00:14:23,280 --> 00:14:24,360 And there we go. 212 00:14:24,360 --> 00:14:25,620 It fades back in. 213 00:14:25,620 --> 00:14:28,350 And then if we touch it again, as you can see, it breaks again. 214 00:14:28,350 --> 00:14:31,020 And then we can walk along this bridge. 215 00:14:31,200 --> 00:14:32,400 And that's pretty neat. 216 00:14:32,400 --> 00:14:38,730 So now we have this trap where when we walk along the wooden beams there's a small little delay. 217 00:14:38,730 --> 00:14:41,430 And then afterwards all the beams break. 218 00:14:41,430 --> 00:14:46,650 So that means if you were to stand on one a little too long, as you can see, they break and you fall 219 00:14:46,650 --> 00:14:47,430 to your death. 220 00:14:47,430 --> 00:14:48,420 Very cool. 221 00:14:50,450 --> 00:14:53,750 So that's the first trap completed thus far. 222 00:14:53,780 --> 00:14:56,960 Keep up the good work and I'll see you in the next lecture.